package safari.controller.buy;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.buy.BuyBoardService;
import safari.model.buy.BuyBoardVO;
import safari.model.buy.BuyCommentVO;
import safari.model.common.ListVO;
import safari.model.member.MemberVO;

public class BuyBoardController extends MultiActionController{
	private BuyBoardService buyBoardService;
	public BuyBoardController(BuyBoardService buyBoardService) {
		this.buyBoardService = buyBoardService;
	}
	/* 글쓰기 페이지로 이동 */
	public ModelAndView goToRegister(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView("buy/write");
	}
	
	
	/* 게시물 등록 */
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response, HttpSession session, BuyBoardVO vo)throws Exception{
		BuyBoardVO bvo = null;
		MemberVO mvo=(MemberVO)session.getAttribute("userInfo");
		System.out.println(mvo);
		if(mvo!=null){ // 세션이 끊어지는 것을 체크
			String id=mvo.getId();
			String name=mvo.getName();
			String email=mvo.getEmail();
			vo.setMemberVO(new MemberVO(id, name, email));
			 System.out.println("컨트롤러 전 vo:"+vo);
			bvo = buyBoardService.register(vo);
			int no = bvo.getNo();
			 System.out.println("no:"+no);
			 System.out.println("컨트롤러 후 vo:"+bvo);
		}else{// null 이라면 홈으로 보낸다.
			new ModelAndView("home");
		}
		/*return new ModelAndView("redirect:/buyboard.do?command=showContentNoHits","no",no);*/
		return new ModelAndView("buy/show_content","bvo",bvo);
	}
	
	/* 댓글 등록 */
	public ModelAndView registerComment(HttpServletRequest request, HttpServletResponse response, HttpSession session, BuyCommentVO bcvo) throws Exception{
		System.out.println("댓글 등록!");
		MemberVO mvo=(MemberVO) session.getAttribute("userInfo");
			String id=mvo.getId();
			bcvo.setMemberVO(new MemberVO(id));
			System.out.println("registerComment: "+bcvo);
			System.out.println("ref: "+bcvo.getBuyBoardVO().getNo());
			System.out.println("level: "+bcvo.getBuyBoardVO().getLevel());
		BuyCommentVO resultBcvo=buyBoardService.registerComment(bcvo);
			System.out.println("댓글등록 컨트롤러:"+resultBcvo);
			/*int no = bcvo2.getNo();//아직 안쓰임
			System.out.println("bcvo2의 no:"+no);*/
			return new ModelAndView("JsonView","bcvo",resultBcvo);
			/*return "redirect:/buyboard.do?command=getCommentList&no="+bcvo.getBuyBoardVO().getNo();*/
	}
	
	/* 댓글 보기 */
	public ModelAndView getCommentList(HttpServletRequest request, HttpServletResponse response,HttpSession session, BuyCommentVO bcvo) throws Exception{
		int bno = Integer.parseInt(request.getParameter("bno"));
		System.out.println("댓글보기 bno:"+bno);
		MemberVO mvo=(MemberVO) session.getAttribute("userInfo");
		String id=mvo.getId();
		System.out.println("세션 아이디 : "+id);
		bcvo.setMemberVO(new MemberVO(id));
		List<BuyCommentVO> comList = buyBoardService.getCommentList(bno);
		System.out.println("comList: "+comList);
		return new ModelAndView("JsonView","comList",comList);
	}
	
	/*댓글 수정*
	
	/* 댓글 삭제 */
	public ModelAndView deleteComment(HttpServletRequest request, HttpServletResponse response,HttpSession session, BuyCommentVO bcvo) throws Exception{
		int no = Integer.parseInt(request.getParameter("no"));
		int bno = Integer.parseInt(request.getParameter("bno"));
		buyBoardService.deleteComment(no);
		List<BuyCommentVO> comList = buyBoardService.getCommentList(bno);
		/*getCommentList(request,response,session,bcvso);
		return new ModelAndView("JsonView","comList",comList);*/
		/*return getCommentList(request,response,session,bcvo);*/
		return new ModelAndView("JsonView","comList",comList);
		/*return "redirect:/freeboard.do?command=freeBoardShowContent&no="+fbvo.getNo();*/
	}
	
/*	public ModelAndView getCommentList(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception{
	int nowPage = Integer.parseInt(request.getParameter("nowPage"));
	System.out.println("댓글보기nowPage:"+nowPage);
	ListVO comList = buyBoardService.getCommentList(nowPage);
	System.out.println("comList"+comList);
	return new ModelAndView("JsonView","comList",comList);
}*/
	
	/* 게시물 상세보기, No Hits */
	public ModelAndView showContentNoHits(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int no = Integer.parseInt(request.getParameter("no"));
		System.out.println("noHit: "+no);
		BuyBoardVO vo = buyBoardService.showContentNoHits(no);
		return new ModelAndView("buy/show_content","bvo",vo);
	}
	
	/* 게시물 상세보기, Hit 증가 */
	public ModelAndView showContentHits(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int no = Integer.parseInt(request.getParameter("no"));
		BuyBoardVO bvo = buyBoardService.showContentHits(no);
		System.out.println("showContent Controller bvo"+bvo);
		return new ModelAndView("buy/show_content", "bvo", bvo);
	}
	
	/* 목록 */
	public ModelAndView getContentList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int nowPage = Integer.parseInt(request.getParameter("nowPage"));
		ListVO list = buyBoardService.getContentList(nowPage);
		System.out.println("list : "+list);
		System.out.println("noPage:"+nowPage);
		return new ModelAndView("buy/list", "list", list);
	}
	
	/* 수정 페이지로 보내기 */
	public ModelAndView goUpdateView(HttpServletRequest request, HttpServletResponse response,BuyBoardVO bvo) throws Exception{
		BuyBoardVO rbvo =buyBoardService.showContentNoHits(bvo.getNo());
		System.out.println("수정goUpdateView:"+rbvo);
		return new ModelAndView("buy/update","bvo",rbvo);
	}
	
	/* 수정 */
	public ModelAndView updateContent(HttpServletRequest request, HttpServletResponse response, BuyBoardVO bvo) throws Exception{
		System.out.println("수정1vo:"+bvo);
		/*int no=Integer.parseInt(request.getParameter("no"));*/
		BuyBoardVO vo = buyBoardService.updateContent(bvo);
		System.out.println("수정2bvo:"+vo);
		return new ModelAndView("buy/show_content", "bvo", vo);
	}
	
	/* 삭제 */
	public ModelAndView deleteConetent(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int no = Integer.parseInt(request.getParameter("no"));
		buyBoardService.deleteContent(no);
		ListVO list = buyBoardService.getContentList(1);
		return new ModelAndView("buy/list","list",list);
	}
	
}
